import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

//连接池具体实现
public class ConnectionPool implements ConnectionService{

    //空闲连接列表
    private List<Connection> freeConnection = new ArrayList<>();
    //正在被使用的连接列表
    private List<Connection> activeConnection = new ArrayList<>();

    private DbBean dbBean;

    //标记连接数量
    private int countConn = 0;

    //初始化线程池
    public void init(){
        if(dbBean == null){
            System.out.println("连接属性不能为空！");
            return ;
        }
        //按照初始化大小创建线程池，并放入空闲连接列表
        for(int i = 0; i < dbBean.getInitConnections(); i++){
            Connection newConnection = newConnection();
            if(newConnection != null){
                freeConnection.add(newConnection);
            }
        }
    }

    //创建Connection连接，newConnection()方法
    public synchronized Connection newConnection(){
        try{
            Class.forName(dbBean.getDriverName());
            Connection connection = (Connection) DriverManager.getConnection(dbBean.getUrl(),dbBean.getUserName(),dbBean.getPassword());
            countConn++;
            return connection;
        }catch (Exception e){
            return null;
        }
    }

    //获取连接
    @Override
    public synchronized Connection getConnection() {
        try{
            Connection connection = null;
            if(countConn < dbBean.getMaxActiveConnections()){
                //创建的连接数小于数据库允许的最大活动线程数

                //列表里还有空闲线程
                if(freeConnection.size() > 0){
                    connection = freeConnection.remove(0);
                }else{
                    //没有空闲线程，但还可以创建
                    connection = newConnection();
                }
                //判断连接是否可用
                boolean available = isAvailable(connection);
                if(available){
                    //可以用，添加到活动线程池里
                    activeConnection.add(connection);
                    countConn++;
                }else{
                    //不能用，再次尝试请求，递归
                    countConn--;
                    connection = getConnection();
                }
            }else{
                //创建的连接数超过了最大值，等待，重试
                wait(dbBean.getConnTimeOut());
                connection = getConnection();
            }
            return connection;
        }catch (Exception e){
            System.out.println("创建出错！");
            return null;
        }
    }

    //判断连接是否可用
    public boolean isAvailable(Connection connection){
        try{
            if(connection == null || connection.isClosed()){
                return false;
            }
        }catch (Exception e){
            //TODO:HANDLE EXCEPTION
        }
        return true;
    }

    //释放连接，回收
    @Override
    public synchronized void releaseConnection(Connection connection) {
        try{
            //1.判断连接是否可用
            if(isAvailable(connection)){
                //2.判断空闲连接数是否达到连接池最大容量
                if(freeConnection.size() < dbBean.getMaxConnections()){
                    //没有满，可以被空闲连接列表回收
                    freeConnection.add(connection);
                }else{
                    //满了，关闭连接
                    connection.close();
                }
                activeConnection.remove(connection);
                countConn--;
                notifyAll();
            }
        }catch (Exception e){
            //TODO:HANDLE EXCEPTION
        }
    }
}
